package com.sjz.han.industry;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sjz.han.industry.activites.WaitSettlementMemberActivity;
import com.sjz.han.industry.base.BaseActivity;
import com.sjz.han.industry.dialogs.NoticeMessgeDialog;
import com.sjz.han.industry.entities.LoignEntity;
import com.sjz.han.industry.fragments.HomeFragment;
import com.sjz.han.industry.fragments.MineFragment;
import com.sjz.han.industry.fragments.TeamFragment;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.JsonUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;


public class MainActivity extends BaseActivity implements View.OnClickListener{
    private LinearLayout main_home_ll,main_team_ll,main_mine_ll;
    private ImageView main_home_img,main_team_img,main_mine_img;
    private TextView main_home_tv,main_team_tv,main_mine_tv;
    private FragmentManager fragmentManager;
    private HomeFragment homeFragment;//首页
    private TeamFragment teamFragment;//新车
    private MineFragment mineFragment;//我的

    private NoticeMessgeDialog noticeMessgeDialog=null;
    @Override
    protected int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initDatas() {
     initFragments();
        if("5".equals(shareUtils.getLevel())){//如果是C2才执行
            getYuQi();
        }
    }

    @Override
    protected void initViews() {
        main_home_ll=(LinearLayout)this.findViewById(R.id.main_home_ll);
        main_team_ll=(LinearLayout)this.findViewById(R.id.main_team_ll);
        main_mine_ll=(LinearLayout)this.findViewById(R.id.main_mine_ll);

        main_home_img=(ImageView)this.findViewById(R.id.main_home_img);
        main_team_img=(ImageView)this.findViewById(R.id.main_team_img);
        main_mine_img=(ImageView)this.findViewById(R.id.main_mine_img);

        main_home_tv=(TextView) this.findViewById(R.id.main_home_tv);
        main_team_tv=(TextView) this.findViewById(R.id.main_team_tv);
        main_mine_tv=(TextView) this.findViewById(R.id.main_mine_tv);
    }

    @Override
    protected void setLisener() {
        main_home_ll.setOnClickListener(this);
        main_team_ll.setOnClickListener(this);
        main_mine_ll.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_home_ll:
                //首页
                setCurrentFragment(0);
                break;
            case R.id.main_team_ll:
                //场所
                setCurrentFragment(1);
                break;
            case R.id.main_mine_ll:
                //我的
                setCurrentFragment(2);
                break;


        }
    }
    @Override
    protected void onRetry() {

    }


    private void initFragments() {
        fragmentManager = getSupportFragmentManager();
        homeFragment = new HomeFragment();
        teamFragment = new TeamFragment();
        mineFragment = new MineFragment();
        fragmentManager.beginTransaction()
                .add(R.id.contentContainer, homeFragment)
                .add(R.id.contentContainer, teamFragment)
                .add(R.id.contentContainer, mineFragment)
                .commit();
        setCurrentFragment(0);
    }
    public void setCurrentFragment(int current_fragment) {
        switch (current_fragment) {
            case 0:
                setMenuSelect(current_fragment);
                fragmentManager.beginTransaction()
                        .show(homeFragment)
                        .hide(teamFragment)
                        .hide(mineFragment)
                        .commit();
                break;
            case 1:
                setMenuSelect(current_fragment);
                fragmentManager.beginTransaction()
                        .show(teamFragment)
                        .hide(homeFragment)
                        .hide(mineFragment)
                        .commit();
                break;
            case 2:
                setMenuSelect(current_fragment);
                fragmentManager.beginTransaction()
                        .show(mineFragment)
                        .hide(homeFragment)
                        .hide(teamFragment)
                        .commit();
                break;

        }
    }

    public void setMenuSelect(int index){
        setMenuNomarl();
        switch (index){
            case 0:
                main_home_img.setImageResource(R.mipmap.main_home_on);
                main_home_tv.setTextColor(ContextCompat.getColor(this,R.color.product_alpha));
                break;
            case 1:
                main_team_img.setImageResource(R.mipmap.main_team_on);
                main_team_tv.setTextColor(ContextCompat.getColor(this,R.color.product_alpha));
                break;
            case 2:
                main_mine_img.setImageResource(R.mipmap.main_wd_on);
                main_mine_tv.setTextColor(ContextCompat.getColor(this,R.color.product_alpha));
                break;


        }
    }
    public void setMenuNomarl(){
        main_home_img.setImageResource(R.mipmap.main_home_off);
        main_team_img.setImageResource(R.mipmap.main_team_off);
        main_mine_img.setImageResource(R.mipmap.main_wd_off);

        main_home_tv.setTextColor(ContextCompat.getColor(this,R.color.tv_color2));
        main_team_tv.setTextColor(ContextCompat.getColor(this,R.color.tv_color2));
        main_mine_tv.setTextColor(ContextCompat.getColor(this,R.color.tv_color2));
    }

    public void getYuQi(){
        OkHttpUtils
                .post()
                .url(ReqestUrl.SFYYQ_NOTICE_URL)
                .addParams("token",shareUtils.getToken())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }
            @Override
            public void onResponse(String response, int id) {

                if(ReqestUrl.rebacRequestJson(response)!=null){
                    LoignEntity entity= JsonUtils.getObject(response,LoignEntity.class);
                    if(entity!=null&&entity.code==0){
                        //        0 没有逾期结算 1  有逾期结算
                        if("1".equals(entity.data.flag)){
                            noticeMessgeDialog = new NoticeMessgeDialog(MainActivity.this);
                            noticeMessgeDialog.setOnItemClickLicener(new NoticeMessgeDialog.OnItemClickLicener() {
                                @Override
                                public void isSure() {
                                    startActivity(new Intent(MainActivity.this, WaitSettlementMemberActivity.class));
                                }
                            });
                            noticeMessgeDialog.show();
                        }else{
                        }

                    }
                }
            }
        });
    }
}
